﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace AutoUpdateTool
{
    public class EncryptParse
    {
        #region DES 加解密
        /// <summary>
        ///  DES加密
        /// </summary>
        /// <param name="pToEncrypt">待加密的字符</param>
        /// <param name="secretKey">加密的key 8位数</param>
        /// <returns></returns>
        public static String DESEncrypt(string pToEncrypt, string secretKey)
        {
            byte[] secretKeyArray = Encoding.UTF8.GetBytes(secretKey.Substring(0, 8));
            byte[] keyIV = secretKeyArray;
            byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, provider.CreateEncryptor(secretKeyArray, keyIV), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return Convert.ToBase64String(mStream.ToArray());
        }

        /// <summary>
        ///  DES解密
        /// </summary>
        /// <param name="pToEncrypt">待解密的字符</param>
        /// <param name="secretKey">解密的key 8位数</param>
        /// <returns></returns>
        public static String DESDecrypt(string pToDecrypt, string secretKey)
        {
            byte[] secretKeyArray = Encoding.UTF8.GetBytes(secretKey.Substring(0, 8));
            byte[] keyIV = secretKeyArray;
            byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, provider.CreateDecryptor(secretKeyArray, keyIV), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return Encoding.UTF8.GetString(mStream.ToArray());
        }

        /// <summary>
        /// 根据特定规则获取 DES 8位数秘钥
        /// </summary>
        /// <param name="value">至少长度24的字符串</param>
        /// <returns></returns>
        public static String GenerateDESKey(string value)
        {
            var prasStr = BitConverter.ToString(new MD5CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(value))).Replace("-", "");
            var v = prasStr.Length <= 24 ? prasStr.PadRight(10 - prasStr.Length, 'x') : prasStr;
            var p1 = v.Substring(2, 2);
            var p2 = v.Substring(8, 2);
            var p3 = v.Substring(16, 2);
            var p4 = v.Substring(20, 2);
            return p1 + p2 + p3 + p4;
        }
        #endregion

        /// <summary>
        /// 获取文件的SHA1码
        /// </summary>
        /// <param name="fileInfo">传入的文件名（含路径及后缀名）</param>
        /// <returns></returns>
        public static String GetFileSHA1Hash(string fileName)
        {
            var result = string.Empty;
            //var hash = SHA256.Create();
            //var hash = MD5.Create();
            var hash = System.Security.Cryptography.SHA1.Create();
            using (var st = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                byte[] hashByte = hash.ComputeHash(st);
                result = BitConverter.ToString(hashByte).Replace("-", "");
            }
            return result;
        }
        /// <summary>
        /// 获取文件的MD5码
        /// </summary>
        /// <param name="fileName">传入的文件名（含路径及后缀名）</param>
        /// <returns></returns>
        public static string GetFileMD5Hash(string fileName)
        {
            var result = string.Empty;
            var hash = System.Security.Cryptography.MD5CryptoServiceProvider.Create();
            using (var st = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                byte[] hashByte = hash.ComputeHash(st);
                result = BitConverter.ToString(hashByte).Replace("-", "");
            }
            return result;
        }

        #region BASE64编解码
        /// <summary>
        /// Base64 解码
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static String Base64Decode(string value)
        {
            if (string.IsNullOrEmpty(value))
                return "";
            byte[] bytes = Convert.FromBase64String(value);
            return Encoding.UTF8.GetString(bytes);
        }

        /// <summary>
        /// Base64 编码
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static String Base64Encode(string value)
        {
            if (string.IsNullOrEmpty(value))
                return "";
            byte[] bytes = Encoding.UTF8.GetBytes(value);
            return Convert.ToBase64String(bytes);
        }
        #endregion
    }
}
